<?php
session_start();
$msg = isset($_SESSION['msg'])?$_SESSION['msg']:null;
if($msg)unset($_SESSION['msg']);
require_once '../providerConstants.php';
echo $htmlHead;
?>
<script>
var firstPageSw = false;
$(document).on('pageinit', function() {
if(firstPageSw)return;
if(!navigator.geolocation){		
	$('body').html("GEO Location Unavailable");
	return;
}
firstPageSw = true;
navigator.geolocation.getCurrentPosition(function(position){		
	var lat = position.coords.latitude;   			
	var lng = position.coords.longitude;   		
	$('#1').html(lat);
	$('#2').html(lng);
	var myLatlng = new google.maps.LatLng(lat, lng);
	var myOptions = {
		zoom : 14,
		center : myLatlng,
		mapTypeId : google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
	var marker = new google.maps.Marker({
		position : myLatlng,
		map : map,
		draggable : true
	});
	google.maps.event.addListener(marker, 'drag', function() {
		lat = marker.position.lat();
		lng = marker.position.lng();
		$('#1').html(lat);
		$('#2').html(lng);
	});

	},function(error){			
		switch(error.code){							
			case error.PERMISSION_DENIED: 									
				$('body').html('Permission Denied');       								
				break;       						
			case error.POSITION_UNAVAILABLE:         								
				$('body').html('Position Unavailable');       								
				break;	   						
			case error.TIMEOUT:       								
				$('body').html('Timeout');      								
				break;      						
			default:       								
				$('body').html('Other Error');    								
			break;					
		}		
	}	
);
$(document).ajaxStart(function(){$.mobile.loading('show');});
$(document).ajaxStop(function(){$.mobile.loading('hide');});
$("#savePost" ).on("pagehide",function(event,ui){
	$("#email").prop('disabled',false);
	$("#phone").prop('disabled',false);
	$("#comment").prop('disabled',false);
	$("#saveButton").prop('disabled',false);
	$("#saveMsg").html('');
});
$("#register" ).on("pagehide",function(event,ui){
	$("#registerEmail").prop('disabled',false);
	$("#registerPhone").prop('disabled',false);
	$("#registerName").prop('disabled',false);
	$("#registerPassword").prop('disabled',false);
	$("#registerButton").prop('disabled',false);
	$("#msg2").html('');
});
$("#checkLoc" ).on("pagehide",function(event,ui){
	$("#checkMsg").html('');
});

});
function register(){
	email = $('#registerEmail').val();
	phone = $('#registerPhone').val();
	name = $('#registerName').val();
	pass = $('#registerPassword').val();
	if(email==''||phone==''||name==''||pass=='')return;
	$.ajax({
		type: "get",
		url: "register.php?email="+email+"&phone="+phone+"&name="+name+"&pass="+pass,
		async: true,
		success: function(msg){
			if(msg=='0'){
				$('#msg2').html('success! check email to confirm.');
	    		$("#registerEmail").prop('disabled', true);
	    		$("#registerPhone").prop('disabled', true);
	    		$("#registerName").prop('disabled',true);
	    		$("#registerPassword").prop('disabled', true);
	    		$("#registerButton").prop('disabled',true);
			}else{
				if(msg='1062'){
					$('#msg2').html('email/phone/password already used!');
				}else{
					$('#msg2').html(msg);
					return;
				}
			}
		},
        error: function(jqXHR,error,errorThrown) {
	        	$('#msg2').html(jqXHR.status+' '+jqXHR.responseText);
	    		$("#registerEmail").prop('disabled', true);
	    		$("#registerPhone").prop('disabled', true);
	    		$("#registerName").prop('disabled',true);
	    		$("#registerPassword").prop('disabled', true);
	    		$("#registerButton").prop('disabled',true);
       	}
              		
	});
}
function savePos(){
	email = $('#email').val();
	phone = $('#phone').val();
	if(email==''||phone=='')return;
	comment = $('#comment').val();
	var t = new Date();	var YYYY = t.getFullYear();
	var MM = ((t.getMonth() + 1 < 10) ? '0' : '') + (t.getMonth() + 1);
	var DD = ((t.getDate() < 10) ? '0' : '') + t.getDate();
	var HH = ((t.getHours() < 10) ? '0' : '') + t.getHours();
	var mm = ((t.getMinutes() < 10) ? '0' : '') + t.getMinutes();
	var ss = ((t.getSeconds() < 10) ? '0' : '') + t.getSeconds();
	var ts = YYYY+'-'+MM+'-'+DD+' '+HH+':'+mm+':'+ss;
//	$('body').html("saveLoc.php?lat="+$('#1').text()+"&lng="+$('#2').text()+"&email="+email+"&phone="+phone+'&comment='+comment+'&ts='+ts);
//	return;
	$.ajax({
		type: "get",
		url: "saveLoc.php?lat="+$('#1').text()+"&lng="+$('#2').text()+"&email="+email+"&phone="+phone+'&comment='+comment+'&ts='+ts,
		async: true,
		success: function(msg){
			if(msg=='0'){
				$('#saveMsg').html('location saved!');
			}else{
				$('#saveMsg').html(msg);
			}
			$("#email").prop('disabled', true);
			$("#phone").prop('disabled', true);
			$("#comment").prop('disabled',true);
			$("#saveButton").prop('disabled', true);
		},
        error: function(jqXHR,error,errorThrown) {
	        if(jqXHR.status==800){
	        	$('#saveMsg').html('email/phone not on file.  please register.');
	        }else{
	        	$('#saveMsg').html(jqXHR.status+' '+jqXHR.responseText);
	        }  
       	}
	});
}
</script>
</head>
<body>
<div data-role="page" id='index'>
	<div data-role="header" data-position="fixed">
		<div data-role="navbar">
			<ul>
				<li><a href="#savePost" data-rel="dialog" data-role="button">Save This Location</a>
				<li><a href="#checkLoc" data-rel="dialog" data-role="button">Somebody's Last Location</a>
				<li><a href="#register" data-rel="dialog" data-role="button">Register</a>
			</ul>
		</div>
	</div>
	<h3 id='h3'><?php if($msg)echo $msg;?></h3>
	<div data-role="content">		
		<div id="msg" style="text-align:center;font-size:70%;">Your approximate location (draggable).</div>
		<div id="map_canvas" style="margin:auto;width:80%;height:320px;"></div>
		<div id='1' style='display:none;'></div>
		<div id='2' style='display:none;'></div>
	</div>
</div>

<div data-role='page' id='savePost'>
    <div data-role="header">
        <h1>Save Location</h1>
    </div>
    <div data-role="content">
    	<input type='text' id='email' x-webkit-speech placeholder='email'/>
    	<input type='text' id='phone' x-webkit-speech placeholder='phone'/>
    	<input type='text' id='comment' x-webkit-speech placeholder='comment'/>
    	<button id='saveButton' onclick='savePos();'>Save</button>
    	<div id='saveMsg'></div>
    </div>
</div>

<?php echo $checkLocHtml;?>

<div data-role='page' id='register'>
    <div data-role="header">
        <h1>Register</h1>
    </div>
    <div data-role="content">
    	<input type='text' id='registerEmail' x-webkit-speech placeholder='email (required)'/>
    	<input type='text' id='registerPhone' x-webkit-speech placeholder='phone (10 digits required)'/>
    	<input type='text' id='registerName' x-webkit-speech placeholder='name (required)'/>
    	<input type='text' id='registerPassword' x-webkit-speech placeholder='password (required)'/>
    	<button id='registerButton' onclick='register();'>Register</button>
    	<div id='msg2'></div>
    </div>
</div>

</body>
</html>